$(document).ready(function () {
    
    $("a.tooltipgrid").tooltip();
    
					
    _.each(modelObj, function (item) {
        var apptTime = item._ApptTime;
        var time = apptTime.split(':');
        var status = parseInt(item.ApptStatusId) == 2 ? "approved" : "completed";
        $("#" + time[0] + "_" + time[1]).parent().addClass(status);
        if ($("#" + time[0] + "_" + time[1]).html()) {
            var newElement = '';
            if ($('.' + time[0] + "_" + time[1]).length) newElement = $('.' + time[0] + "_" + time[1]);
            else newElement = $('<div/>');
            if(status == "approved")
                newElement.html(newElement.html() + '<div class="time-division slot-division approved"><div class="message"><a href="tel:' + item.Phone + '" class="fa fa-phone col-lg-1 col-xs-1 col-sm-1"></a><span>' + item.Name + '</span><span class="pointer pull-right fa fa-close col-lg-2  col-sm-1 col-xs-2" onclick="DeleteAppointment(' + item.Id + ',this)"></span><span class="pointer pull-right fa fa-edit col-lg-2 col-xs-2" onclick="EditAppointment(' + item.Id + ','+item.PatientId+')"></span></div></div>');
            else
                newElement.html(newElement.html() + '<div class="time-division slot-division approved"><div class="message"><a href="tel:' + item.Phone + '" class="fa fa-phone col-lg-1 col-xs-1 col-sm-1"></a><span>' + item.Name + '</span></div></div>');

            newElement.addClass(time[0] + "_" + time[1]);
            if (!$('.' + time[0] + "_" + time[1]).length) {
                $('body').append(newElement);
                $("#" + time[0] + "_" + time[1]).html($("#" + time[0] + "_" + time[1]).html() + '<span class="pull-right"><div class="pointer more-link" onclick="showMoreAppts(\'' + time[0] + '_' + time[1] + '\')">check more</div></span>')
            }
        }
        else {
            $("#" + time[0] + "_" + time[1]).html('<a href="tel:' + item.Phone + '" class="fa fa-phone col-lg-1 col-xs-1 col-sm-1"></a><span>' + item.Name + '</span><span class="pointer pull-right fa fa-close col-lg-2  col-sm-1 col-xs-2" onclick="DeleteAppointment(' + item.Id + ',this)"></span><span class="pointer pull-right fa fa-edit col-lg-2 col-xs-2" onclick="EditAppointment(' + item.Id +  ','+item.PatientId+')"></span>');
        }
    });
    _.each($(".message"), function (item) { if (item.innerHTML === "") $("#" + item.getAttribute("id")).removeClass("message").parent().addClass("no-message") });
    $("body > div").dialog({
        autoOpen: false,
        title: "Patient Details"
    });
});


function EditAppointment(_appointmentId,patientId)
{
    $.ajax({
        type: "POST",
        url: _AbsolutePath + 'Appointment/AddDocApptIdToSession',
        contentType: "application/json",
        data: JSON.stringify({ DoctorApptId: _appointmentId, patientId :  patientId}),
        caller: arguments.callee.name,//gets currently running function name
        dataType: "json", //Cross domain call (even json also work)
        success: function (res) {
            location.href = _AbsolutePath + 'Appointment/EditAppointment';
        },
        error: function (xhr) {
        }
    });
}

function DeleteAppointment(_appointmentId, element) {
    if(confirm("Do you want to delete this appointment?")){
        $.ajax({
            type: "POST",
            url: _AbsolutePath + 'Appointment/DeleteAppointment',
            contentType: "application/json",
            data: JSON.stringify({ DocAppId: _appointmentId }),
            caller: arguments.callee.name,//gets currently running function name
            dataType: "json", //Cross domain call (even json also work)
            success: function (res) {
                
                var rowToDelete = $(element).closest(".message");
                rowToDelete.html("")
                rowToDelete.removeClass("message").parent().addClass("no-message");
                if ($(".time-division.slot-division .message").length == 0) {
                    $(".row.btn-primary,.dhx_scale_holder,#selectedDate").remove();
                    $("#emptyGrid").html("No Appointments are scheduled for this date.");
                }
                SendNotificationForDeletedAppointment(res);
            },
            error: function (xhr) {
            
            }
        });
    }
}

function showMoreAppts(time) {
    $('.ui-dialog > div.' + time).dialog('open');
    $('.ui-dialog.ui-widget.ui-widget-content.ui-corner-all.ui-front.ui-draggable.ui-resizable').css('width', '500px');
    
}

function SendNotificationForDeletedAppointment(DocApptId) {
    $.ajax({
        async: true,
        type: "POST",
        url: _AbsolutePath + 'Appointment/SendEmailforAppointmentDeleted',
        contentType: "application/json",
        data: JSON.stringify({ docApptResponse: DocApptId }),
        caller: arguments.callee.name,//gets currently running function name
        dataType: "json", //Cross domain call (even json also work)
        success: function (res) {

        },
        error: function (xhr) {
        }
    });
}